// void, Obj This

Unit newunit;
Barrack this;
point ptExit;
int level,i,randItem;
ObjList ol;
str nameItem;

this = This.AsBuilding();

// needed for the tutorial message to check this ever started
//EnvWriteInt(.player, "HeroBuilt", 1);

while(.settlement.IsFull)
	Sleep(500);
	
.Progress();

while(.settlement.IsFull)
	Sleep(500);

level = EnvReadInt(.settlement, "levels/Hero");

// Mumificatiion
if(EnvReadString(.settlement, "Mumification")=="researched"){
	level = EnvReadInt(.player, "BestHeroLevel");
	
	ol = GetPlayerUnits(.player, "Hero");
	for(i=0; i<ol.count;i+=1){
		if(ol[i].IsValid())
			if(ol[i].AsHero().inherentlevel > level)
				level = ol[i].AsHero().inherentlevel;
	}
	
	level = (2*level)/3;
	if(level<1) level = 1;
}

if(EnvReadString(.settlement, "Tournaments")=="researched"){
	if(!EnvReadString(.settlement, "OneHero")=="hire"){
		EnvWriteString(.settlement, "OneHero", "hire");	
		level = 10;
	}
}

if(EnvReadString(.settlement, "People's Assembly")=="researched"){
	if(rand(100) < 40){

		randItem = rand(23);

		if(randItem == 0)
				nameItem = "Magic Shoes";
		else if(randItem == 1)
				nameItem = "Battle drums of rage";
		else if(randItem == 2)
				nameItem = "Harp of stamina";
		else if(randItem == 3)
				nameItem = "Sign of the hammer";
		else if(randItem == 4)
				nameItem = "Ghost's skull";
		else if(randItem == 5)
				nameItem = "Wine of vigour";
		else if(randItem == 6)
				nameItem = "Watch Eye II";
		else if(randItem == 7)
				nameItem = "Assassin sword";
		else if(randItem == 8)
				nameItem = "Herb amulet of luck";
		else if(randItem == 9)
				nameItem = "King's Belt";
		else if(randItem == 10)
				nameItem = "Fur gloves of health";
		else if(randItem == 11)
				nameItem = "Finger of death";
		else if(randItem == 12)
				nameItem = "Ash of druid heart";
		else if(randItem == 13)
				nameItem = "Horn of victory";
		else if(randItem == 14)
				nameItem = "Boar teeth";
		else if(randItem == 15)
				nameItem = "Elephant Tusk";
		else if(randItem == 16)
				nameItem = "Gold of resource";
		else if(randItem == 17)
				nameItem = "Food of resource"; 
		else if(randItem == 18)
				nameItem = "Lion's head";
		else if(randItem == 19)
				nameItem = "Healing staff";
		else if(randItem == 20)
				nameItem = "Blue ring";
		else if(randItem == 21)
				nameItem = "Hero speech"; 
		else if(randItem == 22)
				nameItem = "Pouch Gold";
	}
}

ptExit = .GetExitPoint(.settlement.GetCentralBuilding.pos, false);

if (ptExit.x==-1 && ptExit.y==-1) {
	Sleep(rand(100)+100);
	newunit = Place(GetRandomHeroClass(cmdparam, .player), Point(0,0), this.player);
	if(newunit.IsValid)
		newunit.SetLevel(level);

} else {
	Sleep(rand(100)+100);
	newunit = Place(GetRandomHeroClass(cmdparam, .player), ptExit, this.player);
	if(newunit.IsValid)
		newunit.SetCommand("advanceenter", .settlement.GetCentralBuilding);
}

if(newunit.IsValid){
	newunit.AddItem(nameItem);
	newunit.SetLevel(level);
	if(EnvReadString(.player, "Power of Seth") == "researched")
		if(newunit.AsHero.stamina < 15)
			newunit.AsHero.AddBonus(0,0,0,5,0);
	if(EnvReadString(.player, "IncreaseSight") == "yes")
		newunit.SetSight(newunit.sight+400);
}
